package com.holdup;

import com.util.UserUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyFilter implements Filter{
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化filter");
    }
    
    @Override
    public void doFilter(ServletRequest request , ServletResponse response , FilterChain chain) throws IOException, ServletException {
        System.out.println("doFilter=====");
    
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        
        String servletPath = httpRequest.getServletPath();
        System.out.println("servletPath : " + servletPath);
        switch (servletPath){
            case "/pages/404.html":
            case "/pages/admin/login.html":
            case "//pages/admin/login.html":
                chain.doFilter(request, response);
                return;
            default:
                break;
        }
        
        Integer userNum = UserUtil.getUserNum(httpRequest.getSession());
        if (userNum == null){
            httpResponse.sendRedirect("/pages/404.html");
        }else{
            chain.doFilter(request, response);
        }
        
    }
    
    @Override
    public void destroy() {
        System.out.println("销毁filter");
    }
    
}
